package com.joint.web.action.mp;

import com.fz.us.base.util.LogUtil;
import com.joint.base.mp.WxEchoMpServiceImpl;
import com.joint.base.mp.WxMpInCacheConfigStorage;
import com.joint.web.action.BaseController;
import fz.me.chanjar.weixin.common.bean.WxJsapiSignature;
import fz.me.chanjar.weixin.common.exception.WxErrorException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 后台Action类 - 微信首页
 * ============================================================================
 * 版权所有 2014 min_xu。
 * ----------------------------------------------------------------------------
 *
 * @author min_xu
 *
 * @version 1.1 2014-3-31
 */
@Controller("mpAjax")
@RequestMapping("/mp")
public class AjaxAction extends BaseController {
	private static final long serialVersionUID = -5383463207248344967L;

    @Resource
    private WxMpInCacheConfigStorage wxMpInCacheConfigStorage;
    @Resource
    private WxEchoMpServiceImpl wxMpService;

    private WxJsapiSignature jsapiSignature;

	public String execute(){
        return "index";
    }
    @RequestMapping(value = "/ajax-center",method = RequestMethod.GET)
    public String center(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/index";
    }
    @RequestMapping(value = "/ajax-client",method = RequestMethod.GET)
    public String client(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/ajax_client";
    }
    @RequestMapping(value = "/ajax-linkman",method = RequestMethod.GET)
    public String linkman(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/ajax_linkman";
    }
    @RequestMapping(value = "/ajax-knowledge",method = RequestMethod.GET)
    public String knowledge(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/ajax_knowledge";
    }
    @RequestMapping(value = "/ajax-proInfo",method = RequestMethod.GET)
    public String proInfo(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/ajax_proInfo";
    }
    @RequestMapping(value = "/ajax-announce",method = RequestMethod.GET)
    public String announce(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return  "/mp/ajax_announce";
    }
    @RequestMapping(value = "/ajax-proBack",method = RequestMethod.GET)
    public String proBack(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/ajax_proBack";
    }
    @RequestMapping(value = "/ajax-proSignin",method = RequestMethod.GET)
    public String proSignin(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/ajax_proSignin";
    }
    @RequestMapping(value = "/ajax-comment",method = RequestMethod.GET)
    public String comment(HttpServletRequest request,Model model){
        checkJsapi(request,model);
        return "/mp/ajax_comment";
    }

    public Model checkJsapi(HttpServletRequest request,Model model){
        try {
            jsapiSignature =  wxMpService.createJsapiSignature(request.getRequestURL()+"");
            jsapiSignature.setAppId(wxMpInCacheConfigStorage.getAppId());
            model.addAttribute("jsapiSignature", jsapiSignature);
            //LogUtil.info("appId:" + jsapiSignature.getAppId() );
        } catch (WxErrorException e) {
            LogUtil.info(e);
        }
        return model;
    }
    public String users(){return "users";}

    public WxJsapiSignature getJsapiSignature() {
        return jsapiSignature;
    }

    public void setJsapiSignature(WxJsapiSignature jsapiSignature) {
        this.jsapiSignature = jsapiSignature;
    }
}
